package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.base.Page;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SkuManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SkuManageServiceImpl implements SkuManageService {

    @Autowired
    private SkuManageMapper skuManageMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;
    @Autowired
    private SkuImageMapper skuImageMapper;
    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;
    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuInfo(SkuInfo skuInfo) {
        if(null==skuInfo){
            throw new NullPointerException("skuInfo的数据为空");
        }
        skuManageMapper.saveSkuInfo(skuInfo);
        Long skuId = skuInfo.getId();
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        if(CollectionUtils.isEmpty(skuAttrValueList)){
            throw new NullPointerException("SkuAttrValue的数据为空");
        }
        skuAttrValueMapper.saveSkuAttrValueList(skuId,skuAttrValueList);
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        if(CollectionUtils.isEmpty(skuImageList)){
            throw new NullPointerException("skuImageList的数据为空");
        }
        skuImageMapper.saveSkuImageList(skuId,skuImageList);
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        if(CollectionUtils.isEmpty(skuSaleAttrValueList)){
            throw new NullPointerException("skuSaleAttrValueList的数据为空");
        }
        Long spuId = skuInfo.getSpuId();
        skuSaleAttrValueMapper.saveSkuSaleAttrValueList(skuId,spuId,skuSaleAttrValueList);
    }


    @Override
    public Page<SkuInfo> pageList(Long page, Long size, Long category3Id) {
        int offset = (page.intValue() - 1) * size.intValue();
        int intSize = size.intValue();
        List<SkuInfo> pageList  =skuManageMapper.pageList(offset,intSize,category3Id);
        int total =skuManageMapper.countAll(category3Id);

        Page<SkuInfo> skuInfoPage = new Page<>();
        skuInfoPage.setRecords(pageList);
        skuInfoPage.setCurrent(page);
        skuInfoPage.setSize(size);
        skuInfoPage.setTotal((long)total);
        return skuInfoPage;
    }

    @Override
    public void onSale(Long skuId) {
        skuManageMapper.onSale(skuId);
    }

    @Override
    public void cancelSale(Long skuId) {
        skuManageMapper.cancelSale(skuId);
    }

    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        SkuInfo skuInfo = skuManageMapper.selectById(skuId);
        if(null!=skuInfo){
            QueryWrapper<SkuImage> skuImageQueryWrapper = new QueryWrapper<>();
            skuImageQueryWrapper.eq("sku_id",skuId);
            List<SkuImage> skuImages = skuImageMapper.selectList(skuImageQueryWrapper);
            skuInfo.setSkuImageList(skuImages);
        }
        return skuInfo;
    }

    @Override
    public BaseCategoryView getGategoryView(Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.getGategoryView(category3Id);
        return baseCategoryView;
    }

    @Override
    public BigDecimal getSkuPrice(Long skuId) {
     return    skuManageMapper.getSkuPrice(skuId);
    }

    @Override
    public List<BaseAttrInfo> getAttrList(Long skuId) {
       return skuManageMapper.getAttrList(skuId);
    }

    @Override
    public Map getSkuValueIdsMap(Long spuId) {

        //  创建map 集合
        HashMap<Object, Object> map = new HashMap<>();

        List<Map> skuValueIdsMap  =skuManageMapper.selectSkuValueIdsMap(spuId);
        skuValueIdsMap.forEach(map1 -> {
            map.put(map1.get("values_ids"),map1.get("sku_id"));
        });
     return map;
    }
}
